// pages/login/index/index.js
import Config from "../../../main.config";
import {wxRegister} from "../../../service/Common";
import {getStorageSync, setStorageSync} from "../../../utils/Storage";
import {getRegisterDetail} from "../../../service/Enterprise";
import Dialog from "@vant/weapp/dialog/dialog";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userType: -1, //用户类型
        loading: false //登录状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data._options = options;
        if (options.companyId) {
            setStorageSync(Config.storage.invitationInfo, {
                companyId: options.companyId || "",
                companyName: options.companyName || ""
            })
        }
        let userInfo = getStorageSync(Config.storage.userInfo);
        let roleId = userInfo && userInfo.roleId ? parseInt(userInfo.roleId) : null;
        this.setData({userType: options.roleId || roleId || Config.userType.enterprise});
        this.data._userInfo = userInfo;
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /*
    * 监听组件切换事件
    * 根据name值来判断是那种类型用户
    * name="enterprise" 企业类型
    * name="student" 学生类型
    * */
    bindLoginSwitch: function (e) {
        let name = e.currentTarget.dataset.name;
        if (name === "enterprise") {
            this.setData({userType: Config.userType.student})
        } else {
            this.setData({userType: Config.userType.enterprise})
        }
    },


    /*
    * 微信快捷登录
    * */
    onGetPhoneNumber: async function (e) {
        let options = this.data._options;
        try {
            if (e && e.detail && e.detail.errMsg === "getPhoneNumber:ok") {
                let openId = getStorageSync(Config.storage.openId);
                this.setData({loading: true});
                let postData = {
                    iv: e.detail.iv,
                    encryptedData: e.detail.encryptedData,
                    openid: openId
                }
                options.createUser && options.createUser !== "undefined" ? postData.referees = options.createUser : "";
                let token = await wxRegister(postData)
                setStorageSync(Config.storage.token, token);
                await this._loginSuccess();
            }
        } catch (e) {
            if (e.code === 1402) {
                let userType = this.data.userType;
                await Dialog.alert({
                    title: "登录失败",
                    message: "您绑定的手机号已变更，请切换成手机号登录方式"
                })
                wx.navigateTo({
                    url: "/pages/login/tradition/index?type=" + userType
                    + "&createUser=" + options.createUser
                })
            } else if (e.message) {
                await Dialog.alert({
                    title: "登录失败",
                    message: e.message || ""
                })
            }
            console.error(e)
        } finally {
            this.setData({loading: false})
        }
    },


    /*
    * 登录成功除了函数
    * 如果缓存信息userInfo.roleId 存在 说明用户已经注册过
    * 如果不存在 则说明用户首次注册
    * 如果首次注册的是企业用户 则下一步跳转 企业、员工身份选择
    *
    * 如果是企业用户还需要判断是否审核通过
    * 如果没有通过 需要跳转到审核页面
    * */
    _loginSuccess: async function() {
        let userType = this.data.userType;
        let userInfo = this.data._userInfo;
        let options = this.data._options;
        let userId = userInfo && userInfo.userId ? userInfo.userId : null;
        let roleId = userInfo && userInfo.roleId ? parseInt(userInfo.roleId) : null;
        if (roleId) { //已注册
            switch (userType) {
                case 6:
                    let detail = await getRegisterDetail({createUser: userId});
                    if (detail.status !== 2) { //未通过审核
                        wx.redirectTo({
                            url: "/packageEnterprise/pages/status/index"
                        })
                    } else {
                        wx.redirectTo({url: "/pages/index/index"});
                    }
                    setStorageSync(Config.storage.registerInfo, detail)
                    break
                case 7:
                case 8:
                    wx.redirectTo({url: "/pages/index/index"});
                    break
                default:
                    break
            }
        } else {
            switch (userType) {
                case 6:
                case 7:
                    if (options.companyId) { //如果从公司信息跳转 则直接进入创建用户页面
                        wx.redirectTo({url: "/packagePersonnel/pages/info/index?todo=create"})
                    } else {
                        wx.redirectTo({url: "/pages/login/select/index"})
                    }
                    break
                case 8:
                    wx.redirectTo({url: "/packageStudent/pages/info/index?todo=create"})
                    break
                default:
                    break
            }
        }
    },

    /*
    * 跳转普通登录页面
    * */
    bindNavigatorToTradition: function () {
        let userType = this.data.userType;
        let options = this.data._options;
        wx.navigateTo({
            url: "/pages/login/tradition/index?type=" + userType
            + "&createUser=" + options.createUser
        })
    }
})